<!--
 * @Descripttion:人员列表
 * @version:
 * @Author: ada.wu
 * @Date:2024-03-11 11:26:44
 * @LastEditors: ada.wu
 * @LastEditTime: 2024-04-12 09:09:00
-->
<template>
  <div>
    <van-pull-refresh v-model="isQuery" @refresh="downCallback">
      <van-list v-model:loading="isQuery" :finished="listFinished" @load="onLoadPage">
        <van-cell-group>
          <van-cell v-for="(item,index) in listData" :key="index" :title="item.evaluatedName" is-link @click="goExam(item)">
            <template #value>
              <!-- 1-已评价，2-未评价 -->
              <img v-if="item.status=='1'" src="/@/assets/employee/check.svg" class="v-m">
            </template>
          </van-cell>
        </van-cell-group>
        <template #finished>
          <van-divider v-if="listData.length">没有更多了</van-divider>
        </template>
      </van-list>
    </van-pull-refresh>
    <van-empty v-if="listData.length==0&&!isQuery" description="暂无查询结果…" :image="getAssets('empty.svg')" />
  </div>
</template>
<script lang="ts">
import { List, showDialog, Empty, PullRefresh, Divider } from "vant";

//util
import { onMounted, reactive, toRefs, ref, nextTick } from "vue";
import { useRoute, useRouter } from "vue-router";
import { PageEnum } from "/@/enums/pageEnum";
import { getNewObj } from "/@/util";
import { useStore } from "/@/store";
import storageUtil from "/@/util/storageUtil";

// import { useSystemsStore } from "/@/store/modules/systems";
// console.log("useSystemsStore==",useSystemsStore())
//api
import { personList } from "/@/api/ExamApi";
export default {
  name: "employee",
  components: {
    [List.name]: List,
    [Empty.name]: Empty,
    [Divider.name]: Divider,
    [PullRefresh.name]: PullRefresh,
  },
  setup() {
    const store = useStore();

    console.log("store==", store);
    const router = useRouter();
    const route = useRoute();
    let { valuatorUserId = "", activityId = "" } = route.query;

    const state = reactive({
      dateShow: false,
      dateTime: "" as any,
      dateTimeType: "",
      companyMap: <any>{},
      familyMemberName: "",
      queryParam: {
        valuatorUserId, //评价者userId不能为空
        activityId, //活动ID
        evaluatedName: "", //被评价者名称，左like
        status: "", //1-已评价，2-未评价
        pageNumber: 1, //当前页
        pageSize: 100, //分页大小
      },
      totalElements: 0,
      listData: <any>[],
      listFinished: false,
      isQuery: false,
    });
    onMounted(() => {
      nextTick(() => {});
    });
    //加载时执行
    const onLoadPage = () => {
      queryList();
    };
    const handleReset = () => {
      downCallback();
    };

    const downCallback = () => {
      resetList();
      queryList();
    };

    const queryList = () => {
      state.isQuery = true;
      let { queryParam } = getNewObj(state);
      personList({
        ...queryParam,
        systemCode: storageUtil.getSystemCode(),
      })
        .then((res: any) => {
          if (res) {
            state.listData = [...state.listData, ...res.list];
            if (res.list.length < state.queryParam.pageSize) {
              state.listFinished = true;
            } else {
              state.queryParam.pageNumber++;
            }
          } else {
            state.listData = [];
            state.listFinished = true;
          }
        })
        .catch(() => {
          state.listFinished = true;
        })
        .finally(() => {
          state.isQuery = false;
        });
    };

    const resetList = () => {
      state.isQuery = false;
      state.listData = [];
    };

    const goExam = async (item: any) => {
      let urlParam = {
        ...item,
        activityId,
        valuatorUserId, //评价者
      };
      //获取试卷列表
      //0-未开始，1-已阅读，2-进行中，3-已完成
      let res = await store.dispatch("exam/getExamList", {
        activityId,
        valuatorUserId, //评价者
        evaluatedUserId: item.evaluatedUserId, //被评价者
        examName: "",
        status: "",
      });
      // ErrorToast('未配置问卷信息，请联系管理员')
      if (res && res.list.length) {
        //一条试卷  判断答题状态
        if (res.list.length == 1) {
          //答卷详情
          if (res.list[0].status == "3") {
            router.push({
              path: PageEnum.EXAM_DETAIL,
              query: {
                ...urlParam,
                ...res.list[0],
              },
            });
          } else {
            router.push({
              path: PageEnum.EXAM,
              query: {
                ...urlParam,
                ...res.list[0],
              },
            });
          }
        } else {
          //多个试卷 直接到试卷列表
          router.push({
            path: PageEnum.EXAM_LIST,
            query: urlParam,
          });
        }
      } else {
        showDialog({ message: "未配置问卷信息，请联系管理员" });

        // Toast.fail("未配置问卷信息，请联系管理员");
      }
    };

    return {
      ...toRefs(state),
      goExam,
      downCallback,
      handleReset,
      onLoadPage,
    };
  },
};
</script>

<style lang="scss" scoped>
::v-deep(.van-cell) {
  align-items: center;
}
</style>
